//
//  SearchViewController.m
//  B段项目
//
//  Created by 徐茂怀 on 15/11/9.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "SearchViewController.h"
#import "XMHNetWorkongMethods.h"
#import "FitHeader.h"
#import <UIImageView+WebCache.h>
#import "NSXFirstPageWEBViewController.h"
@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate>

@end
static NSInteger p = 1;
@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [NSMutableArray array];
     [self getData];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    UIView *v = [[UIView alloc]initWithFrame:CGRectZero];
    _tableView.tableFooterView = v;
    [self.view addSubview:_tableView];
    
    _progress = [[MBProgressHUD alloc]initWithView:self.view];
    _progress.labelText = @"加载中,不要着急";
    [_progress setMode:MBProgressHUDModeIndeterminate];
    [_progress show:YES];
    _progress.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_progress];
    
   _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       _tableView.userInteractionEnabled = NO;
       [_dataArray removeAllObjects];
       p = 1;
       [self getData];
   }];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        p += 1;
        [self getData];
    }];
    [self getData];
}

-(void)getData
{
    NSString *page = [NSString stringWithFormat:@"%ld",p];
    NSString *str = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.list.php?keywords=%@&&p=%@&pagesize=12",_name,page];
    [XMHNetWorkongMethods getDataVyString:str BodyString:nil WithDataBlock:^(id data) {
        NSArray *array = [data objectForKey:@"results"];
        for (NSDictionary *dic in array) {
            [_dataArray addObject:dic];
        }
        if (!array.count) {
            [_tableView.footer endRefreshingWithNoMoreData];
        }
else
{
        [_progress removeFromSuperview];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [_tableView reloadData];
    _tableView.userInteractionEnabled = YES;
}
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * FitHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[SearchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (![[_dataArray[indexPath.row]objectForKey:@"title"]isEqualToString:@""]) {
        cell.titleLabel.text = [_dataArray[indexPath.row]objectForKey:@"title"];
    }
    if (![[_dataArray[indexPath.row]objectForKey:@"age"]isEqualToString:@""]) {
        cell.ageLabel.text = [NSString stringWithFormat:@"人群:%@",[_dataArray[indexPath.row]objectForKey:@"age"]];
    }
    if (![[_dataArray[indexPath.row]objectForKey:@"category"]isEqualToString:@""]) {
        cell.categoryLabel.text = [NSString stringWithFormat:@"分类:%@",[_dataArray[indexPath.row]objectForKey:@"category"]];
    }
    if (![[_dataArray[indexPath.row]objectForKey:@"effect"]isEqualToString:@""]) {
        cell.effectLabel.text = [NSString stringWithFormat:@"功效:%@",[_dataArray[indexPath.row]objectForKey:@"effect"]];
    }
    [cell.FirstPageImageView sd_setImageWithURL:[NSURL URLWithString:[_dataArray[indexPath.row]objectForKey:@"thumb"]]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSXFirstPageWEBViewController *webvc = [[NSXFirstPageWEBViewController alloc]init];
    webvc.idString = [_dataArray[indexPath.row]objectForKey:@"ID"];
    webvc.modelString = [_dataArray[indexPath.row]objectForKey:@"title"];
    [self.navigationController pushViewController:webvc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
